/**
 * 微信关注回复
 */

options.fileUpload = function(sender) {
	$.ajaxFileUpload({
		url : 'fileUpload', // 用于文件上传的服务器端请求地址
		type : 'post',
		data : {
			"folder" : "app/",
			"extension" : ".png"
		}, // JSON格式
		secureuri : false, // 一般设置为false
		fileElementId : 'fileIcon', // 文件上传空间的id属性 <input type="file" id="file"
		// name="file" />
		dataType : 'json', // 返回值类型 一般设置为json
		global : true,
		success : function(response, status) { // 服务器成功响应处理函数

			if (response.media_id) {
				$('#imageMediaId').val(response.media_id);
				$('#mediaId').val(response.media_id);
			}
		},
		error : function(data, status, e) {// 服务器响应失败处理函数
			alert(e);
		}
	});
}

options.getMaterial = function(pageIndex) {
	$.ajax({
		url : 'getMaterial',
		type : 'post',
		data : {
			pageIndex : pageIndex
		},
		success : function(response) {

			var _html = '';
			if (response.total_count > 0) {
				$('#item_total').text(response.total_count);
				for (var i = 0; i < response.item.length; i++) {

					var item = response.item[i];
					console.log(item);
					_html += '<tr>';
					var firstOne = item.content.news_item[0];
					_html += '<td>' + firstOne.title + '</td>';
					_html += '<td><img style="width:50px;height:30px;" src="'
							+ firstOne.thumb_url + '" /></td>';
					_html += '<td>' + firstOne.digest + '</td>';
					_html += '<td><input type="radio" value="' + item.media_id
							+ '" name="optionsRadios"></td>';

					_html += '</tr>';
				}
			}

			$('#newsContent').html(_html);
		},
		error : function(result) {
			_showInfoMessage("操作失败。", 'error');
		}
	});
}

options.goBack = function() {

	location.href = 'msgReply';
}

$(document).ready(function() {
	var _submiting1 = false;
	$('#myForm').validator({
		rules : {},
		fields : {
			'#replyType' : 'required'
		},
		valid : function(form) {
			if (_submiting1) {

				return;
			}
			_submiting1 = true;

			FormSubmit(form, function(response) {
				_showInfoMessage("保存成功。", 'success');
				options.goBack();
				// _submiting1 = false;
			});
		}
	});

	$('#btnSave').click(function() {
		var replyType = $('#replyType').val();
		if (replyType == 'keyword' && $('#keyword').val() == '') {
			_showInfoMessage('关键字不能为空。', 'error');
			return;
		}

		var msgType = $('#msgType').val();
		if (msgType == 'text') {
			if ($('#replyText').val() == '') {
				_showInfoMessage('文本内容不能为空。', 'error');
				return;
			}
		} else if (msgType == 'image') {
			if ($('#imageMediaId').val() == '') {
				_showInfoMessage('图片内容不能为空。', 'error');
				return;
			}

		} else if (msgType == 'news') {
			if ($('#newsMediaId').val() == '') {
				_showInfoMessage('图文内容不能为空。', 'error');
				return;
			}

		}

		$('#myForm').submit();
		return false;

	});

	$('#btnClose').click(function() {
		options.goBack();
	});

	showKeyword($('#replyType').val());
	$('#replyType').change(function() {
		showKeyword(this.value);
	});

	function showKeyword(v) {
		if (v == 'keyword') {
			$('#keywordContainer').show();
		} else {
			$('#keywordContainer').hide();
		}
	}
	showMsgType($('#msgType').val());
	$('#msgType').change(function() {

		showMsgType(this.value);
	});
	function showMsgType(v) {
		if (v == 'text') {
			$('#replyTextContainer').show();
			$('#picContainer').hide();
			$('#newsContainer').hide();
		} else if (v == 'image') {
			$('#replyTextContainer').hide();
			$('#picContainer').show();
			$('#newsContainer').hide();
		} else if (v == 'news') {
			$('#replyTextContainer').hide();
			$('#picContainer').hide();
			$('#newsContainer').show();
		}
	}

	$('#fileIcon').change(function() {
		options.fileUpload(this);
	});
	$('#btnSelectNews').click(function() {

		$('#showNewsModal').modal('show');
		options.getMaterial(1);
	});
	$('#btnSaveNews').click(function() {

		var selectedItem = $('#newsContent').find('input[type=radio]:checked');
		if (selectedItem.length == 0) {
			_showInfoMessage('请选择一个图文素材。', 'error');
			return;
		}

		$('#newsMediaId').val(selectedItem.val());
		$('#mediaId').val(selectedItem.val());

		$('#showNewsModal').modal('hide');
	});
});